<template>
	<view class="detail">
		<view class="container">
			<!-- 			<unicloud-db :where="`_id=='${artid}'`" v-slot:default="{data, loading, error, options}" :getone="true"
				collection="zyqc_art">
				{{data}}

			</unicloud-db> -->
			<view v-if="loadState">
				<u-skeleton rows="3" title avatar loading animate rowsHeight='30'></u-skeleton>
			</view>
			<view v-else>
				<view class="title">{{detailObj.title}}</view>
				<view class="userinfo">
					<view class="avatar">
						<!-- 					<image :src="detailObj.user_id[0].avatar_file.url" mode="aspectFill"></image> -->
						<image
							:src="detailObj.user_id[0].avatar_file?detailObj.user_id[0].avatar_file.url:'https://mp-353864e0-55e9-470c-ba50-fc62e6999017.cdn.bspapp.com/cloudstorage/af7573fa-8064-48f7-a439-d32bf1af2c7c.jpg'"
							mode="aspectFill">
					</view>
					<view class="text">
						<view v-if="detailObj.user_id[0].nickname !=''" class="name">
							{{detailObj.user_id[0].nickname ||'游客'}}
						</view>
						<view v-else class="name">{{detailObj.user_id[0].username || item.user_id[0].mobile||'游客'}}
						</view>
						<view class="small">
							<uni-dateformat :date="detailObj.publish_date" format="yyyy年MM月dd日 hh:mm:ss">
							</uni-dateformat> · 发布于{{detailObj.city||'火星'}}
						</view>
					</view>
				</view>
				<view class="content">
					<u-parse :content='detailObj.articleContent' :tagStyle="artStyle"></u-parse>
				</view>

				<view class="like">
					<view class="btn" @click="clickLike" :class="detailObj.isLike ? 'active':''">
						<uni-icons type="star-filled" color="#8adcff" size="30"></uni-icons>
						<text v-if="detailObj.like_count>0">{{detailObj.like_count}}</text>
					</view>
					<view class="users">
						<image src="" mode="aspectFill"></image>
					</view>
					<view class="text"><text class="num">{{detailObj.view_count}}</text>人看过</view>
				</view>
				<!-- 公司标识 -->
				<view class="copyright">
					州游黔程提供计算服务<br />
					COPYRIGHT©2022-2023<br />
					ZhouYouQianCheng
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	const db = uniCloud.database();
	const utilsObj = uniCloud.importObject('urilsObj', {
		customUI: true //取消自动展示的交互提示界面（加载中）
	})
	import {
		store
	} from '@/uni_modules/uni-id-pages/common/store.js'
	export default {
		data() {
			return {
				loadState: true,
				artid: '',
				artStyle: {
					p: 'line-hight:1.7em;font-size:32rpx;padding-bottom:10rpx',
					img: 'margin:10rpx 0;'
				},
				detailObj: []
			};
		},
		onLoad(e) {
			if (!e.articleId) {
				this.errFun()
				return
			}
			// console.log(e);
			uni.setNavigationBarTitle({
				title: this.detailObj.title
			})
			this.artid = e.articleId
			this.getArtShow()
			this.readUpdate()
		},
		methods: {
			// 点赞操作
			clickLike() {
				if (!store.hasLogin) {
					uni.showModal({
						title: '登录后才可以进行点赞操作',
						success: res => {
							if (res.confirm) {
								uni.navigateTo({
									url: '/uni_modules/uni-id-pages/pages/login/login-withpwd'
								})
							}
						}
					})
					return
				}
				let time = Date.now();
				if (time - this.likeTemp < 2000) {
					uni.showToast({
						title: '操作太频繁，请稍后重试...',
						icon: 'none'
					})
					return
				}
				// 前端无感操作
				this.detailObj.isLike ? this.detailObj.like_count-- : this.detailObj.like_count++;
				this.detailObj.isLike = !this.detailObj.isLike;
				console.log(this.detailObj.isLike, 565656);
				this.likeTemp = time;
				// 调用点赞方法
				this.likeFun()
			},

			// 点赞操作数据库的方法
			async likeFun() {
				let count = await db.collection('zyqc_like')
					.where(`article_id=="${this.artid}" && user_id==$cloudEnv_uid`).count()
				if (count.result.total) {
					db.collection('zyqc_like').where(`article_id=="${this.artid}" && user_id==$cloudEnv_uid`).remove();
					utilsObj.operation('zyqc_art', 'like_count', this.artid, -1)
				} else {
					db.collection('zyqc_like').add({
						article_id: this.artid
					})
					utilsObj.operation('zyqc_art', 'like_count', this.artid, 1)
				}
			},
			// 修改阅读量
			readUpdate() {
				utilsObj.operation('zyqc_art', 'view_count', this.artid, 2).then(res => {
					console.log(res);
				})
			},
			// 错误处理
			errFun() {
				uni.showToast({
					title: '参数错误',
					icon: 'none'
				})
				setTimeout(() => {
					uni.reLaunch({
						url: "/pages/index/index"
					})
				}, 1000)
			},

			//请求详情页主要内容数据
			getArtShow() {
				let artTemp = db.collection('zyqc_art').where(`_id == "${this.artid}"`).getTemp();
				let userTemp = db.collection('uni-id-users').field('_id,username,nickname,avatar_file').getTemp();
				let likeTemp = db.collection("zyqc_like").where(`article_id=="${this.artid}" && user_id==$cloudEnv_uid`)
					.getTemp();

				let temrArr = [artTemp, userTemp];
				if (store.hasLogin) temrArr.push(likeTemp)

				db.collection(...temrArr).get({
					getOne: true
				}).then(res => {
					// console.log(res, 888);
					if (!res.result.data) {
						this.errFun();
						return;
					}
					this.loadState = false;

					let isLike = false;
					if (store.hasLogin) isLike = res.result.data._id.zyqc_like.length ? true : false;
					// console.log(res.result.data._id.zyqc_like.lenght, isLike, 989898);
					res.result.data.isLike = isLike;
					this.detailObj = res.result.data;
					uni.setNavigationBarTitle({
						title: this.detailObj.title
					})
				}).catch(err => {
					this.errFun()
				})

			}

		}
	}
</script>

<style lang="scss">
	.detail {
		background: #f8f8f8;
		min-height: calc(100vh - var(--window-top));

		.container {
			padding: 30rpx;
			background: #fff;

			.title {
				font-size: 46rpx;
				color: #333;
				line-height: 1.4em;
				font-weight: 600;
			}

			.userinfo {
				padding: 20rpx 0 50rpx;
				display: flex;
				align-items: center;

				.avatar {
					width: 60rpx;
					height: 60rpx;
					padding-right: 15rpx;
					border-radius: 50%;

					image {
						width: 100%;
						height: 100%;
						border-radius: 50%;
					}
				}

				.text {
					font-size: 28rpx;
					color: #555;

					.small {
						font-size: 20rpx;
						color: #999;
					}
				}
			}

			.content {}

			.like {
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 80rpx 50rpx 50rpx;

				.btn {
					width: 260rpx;
					height: 120rpx;
					background: #e4e4e4;
					border-radius: 100rpx;
					color: #8adcff;
					display: flex;
					justify-content: center;
					align-items: center;
					flex-direction: column;
					font-size: 28rpx;

					.iconfont {
						font-size: 50rpx;
					}

					&.active {
						background: #6c5ddc;
					}
				}

				.text {
					font-size: 26rpx;
					color: #666;

					.num {
						color: #8adcff
					}

					.spot {
						padding: 0 10rpx;
					}
				}

				.users {
					display: flex;
					justify-content: center;
					padding: 30rpx 0;

					image {
						width: 50rpx;
						height: 50rpx;
						border-radius: 50%;
						border: 3px solid #fff;
						margin-left: -20rpx;
					}
				}
			}

		}
	}
</style>
